// 实现这个项目的构建任务
const { 
    src,dest,
    series,  // 串行任务
    parallel //并行任务
} = require('gulp')

const swig = require('gulp-swig')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
// sass.compiler = require('node-sass');
const sourcemaps = require('gulp-sourcemaps');
const del = require('del')
const bs = require("browser-sync").create();
// const plugins = require('gulp-load-plugins')()
const htmlMinify = require('gulp-html-minify')
const cssMinify = require('gulp-clean-css')
const useref = require('gulp-useref')
const gulpif = require('gulp-if')
const uglify = require('gulp-uglify')
const imagemin = require('gulp-imagemin');
const eslint = require('gulp-eslint');
const cliOptions = require('minimist')(process.argv);
const { exec } = require('child_process');
const path = require('path')


// const cwd = process.cwd()
// console.log(__filename);
// console.log(__dirname);
// console.log(cwd);
console.log(cliOptions);


//Lint the styles & scripts files.
// const lint = 

//Clean the `dist` & `temp` files.
const clean = async ()=> await del(['temp', 'dist']);

//Compile the styles & scripts & pages file.
const compileStyle = ()=> src('./src/assets/styles/**.scss')
                        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
                        .pipe(dest('./temp/assets/styles'));

const compileJs = ()=> src('./src/assets/scripts/**.js')
                        // .pipe(eslint())
                        // .pipe(eslint.format())
                        // .pipe(eslint.failAfterError())
                        .pipe(sourcemaps.init())
                        .pipe(babel({
                            presets: [require('@babel/preset-env')]
                        }))
                        .pipe(dest('./temp/assets/scripts'));

const compileHTML =()=>  src('./src/**.html')
                    .pipe(swig({defaults: { cache: false }}))
                    .pipe(useref({
                        searchPath: ['./temp','.']
                    }))
                    .pipe(dest('./temp'));

const compile = parallel(compileStyle,compileJs,compileHTML)

/**
Runs the app in development mode with a automated server.
 * options
- `open`: Open browser on start, Default: `false`
- `port`: Specify server port, Default: `2080`
 */
const serve = series(
    clean,
    compile,
    done=>{
        bs.init({
            open: cliOptions.open === "true" ,
            port: cliOptions.port || 2080,
            server: {
                baseDir: ["temp","src","public"],
            }
        })
        bs.watch('src').on('change', _path=>{
            console.log(_path);
            if(/.html$/.test(_path)) compileHTML()
            if(/.js$/.test(_path)){
                compileJs()
            }
            if(/.scss$/.test(_path)) {
                compileStyle()
            }
            bs.reload()
        })

        done()
    }
)
const minImg = ()=> src('./src/assets/images/**')
                    .pipe(imagemin())
                    .pipe(dest('./dist/assets/images/'))

const font = ()=> src('./src/assets/fonts/**')
                    .pipe(dest('./dist/assets/fonts/'))

const minAll = ()=> src(['temp/**','public/**'])//
                    .pipe(gulpif('*.html',htmlMinify()))
                    .pipe(gulpif('*.css',cssMinify()))
                    .pipe(gulpif('*.js',uglify()))
                    .pipe(dest('dist'))

/**
 * Builds the app for production to the `dist` folder. It minify source in production mode for the best performance.
 * options
- `production`: Production mode flag, Default: `false`
- `prod`: Alias to `production`
 */
const build = series(
    clean,
    compile,

    parallel(
        minAll,
        minImg,
        font
    )
)

/**
 * Running projects in production mode.
 * options：
 * - `open`: Open browser on start, Default: `false`
- `port`: Specify server port, Default: `2080`
 */
const start = series(
    build,
    ()=>bs.init({
        open: cliOptions.open === "true" ,
        port: cliOptions.port || 2080,
        server: {
            baseDir: "dist"
        }
    })
)

//Deploy the `dist` folder to [GitHub Pages](https://pages.github.com).
/**
 * options： `branch`: The name of the branch you'll be pushing to, Default: `'gh-pages'`
 */
// const deploy = 

module.exports = {
    compile,
    clean,
    // lint,
    serve,
    build,
    start,
    // deploy,
}